<template>
    <div>
  <table class="table table-bordered">
    <h1>房号添加</h1>
            <tbody>
                <tr>
                    <td>选择房型</td>
                    <td>  <select v-model="info.lid">
            <option value="">请选择</option>
            <option :value="item.nid" v-for="item in level" :key="item.nid">{{item.oname}}</option>  
        </select></td>
                </tr>
                 <tr>
                    <td>房号</td>
                    <td><input type="text" v-model="info.num"></td>
                </tr>
                  <tr>
                    <td>房间描述</td>
                    <td><input type="text" v-model="info.introduce"></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" value="1" name="aa" v-model="info.state">上架
                        <input type="radio" value="0" name="aa" v-model="info.state">下架
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
        {{info}}
    </div>
</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
import moment from 'moment';
import router from '@/router';

let level:any=ref([]);

let info:any=ref({
  "id": 0,
  "num": "",
  "introduce": "",
  "state": "",
  "lid": ""
})


onMounted(()=>{
    GetLevel();
})

//下拉
const GetLevel=()=>{
    axios({
        url:'https://localhost:7058/api/Num/Getnumlevel',
        method:'get',
    })
    .then((res)=>{
        console.log(res);
        level.value=res.data
    })
    .catch((err)=>{
        console.log(err);
    })
}

//添加
const add=()=>{
    if(info.value.num==""){
        alert("房号不能为空");
        return;
    }
     
     if(info.value.introduce==""){
        alert("房间描述不能为空");
        return;
    }
    axios({
        url:'https://localhost:7058/api/Num/Addnum',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("房号已存在");
            return;
        }
        if(res.data>0){
            alert("添加成功")
            location.href='/numshow'
        }
        
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>